﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;
using System.Reflection;
using Newtonsoft.Json;

namespace E02
{
    /// <summary>
    /// 同步销售账户
    /// </summary>
    public class A07 : ISYNChildGet
    {
        public void Code(ISYN handler, ref CodeResult result, SYNRight user, SerialType st, String baseNo, String billId)
        {
            try
            {
                result.Code = "E02.A07";
                List<E02A07P> ht = new List<E02A07P>();
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@FID", SqlDbType.NVarChar, 50);
                sps[0].Value = billId;
                string sql = " select a.FID BillId,FBillNo Number,case when FDate = '' then '1990-01-01 00:00:00' when FDate IS null then '1990-01-01 00:00:00' else FDate end BuildDate,b.F_102 CusName,FNOTE Remark," +
                             " FDecimal4 Facility,FDecimal PayTotal,FDecimal1 ConsumeTotal,FDecimal2 NowLeftMoney," +
                             " FDecimal5 CanUseMoney,FComboBox PassMode,FBillNo_SRC FileNo," +
                             " c.FName PayWay,d.FName BanlanceWay,FText2 GetPassWord,FID_SRC SrcID,FClassID_SRC SrcType,isnull(e.FName,'') SalesArea,f.FName Biller,g.FName Checker " +
                             " ,(select FText5 from CardServer.dbo.KEW_DataBaseEntry where FNumber = " + baseNo + " ) FiUnitID from dbo.KEW_SalesAccount a" +
                             " left join t_Organization b on a.FBASE = b.FItemID  left join t_Settle c on a.FBASE1 = c.FItemID " +
                             " left join t_SubMessage d on a.FBASE5 = d.FInterID  left join t_SubMessage e on e.FInterID = a.FBase6 " +
                             " left join t_user f on f.FUserID = a.FBiller and a.FBiller <> 0 " +
                             " left join t_user g on g.FUserID = a.FUser and a.FUser <> 0 where a.FID = @FID";
                sql = sql + " select a.FEntryID BillId,b.ftext ProInfo,a.FText ExecPolicy,a.FDecimal3 TaxPrice from dbo.KEW_SalesAccountEntry a " +
                            " left join KEW_ProductsEntry b on a.FBASE2 = b.FID where a.FID = @FID";
                Recordset rs = handler.Data.ExeProcedure(baseNo, sql, sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows.Count == 1)
                    {
                        DataRow row = rs.Data.Tables[0].Rows[0];
                        string s = row["CusName"].ToString();
                        if (row["CusName"] == null || row["CusName"].ToString().Equals(""))
                        {
                            result.Result = EnumResult.None;
                            result.Message = String.Format("销售账户数据提取错误:{0}", "请先配置基础资料客户对应关系！编号：" + row["Number"].ToString());
                            return;
                        }

                        E02A07P e02A07P = new E02A07P();
                        for (int i = 0; i < rs.Data.Tables[0].Columns.Count; i++)
                        {
                            DataColumn dc = rs.Data.Tables[0].Columns[i];
                            ReflectUtil.setValue(e02A07P, dc.ColumnName, row[dc.ColumnName].ToString());
                        }

                        List<E02A07E> OrderList = new List<E02A07E>();

                        foreach (DataRow entry in rs.Data.Tables[1].Rows)
                        {
                            E02A07E e02A07E = new E02A07E();
                            for (int j = 0; j < rs.Data.Tables[1].Columns.Count; j++)
                            {
                                DataColumn dc = rs.Data.Tables[1].Columns[j];
                                ReflectUtil.setValue(e02A07E, dc.ColumnName, entry[dc.ColumnName].ToString());
                            }
                            OrderList.Add(e02A07E);
                        }
                        e02A07P.ListAdjustPrice = JsonConvert.SerializeObject(OrderList);
                        ht.Add(e02A07P);
                        result.Content = JsonConvert.SerializeObject(ht);
                    }
                    else
                    {
                        result.Result = EnumResult.None;
                    }
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("销售账户数据提取错误:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("销售账户数据提取错误:{0}", ex.Message);
            }
        }

    }

    /// <summary>
    /// 销售账户
    /// </summary>
    public class E02A07P
    {
        public String BillId = String.Empty; //单据ID
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 下单日期
        /// </summary>
        public String BuildDate;
        /// <summary>
        /// 客户名称
        /// </summary>
        public String CusName;
        /// <summary>
        /// 备注
        /// </summary>
        public String Remark;
        /// <summary>
        /// 授信额度
        /// </summary>
        public Decimal Facility;
        /// <summary>
        /// 付款总计
        /// </summary>
        public Decimal PayTotal;
        /// <summary>
        /// 消费总计
        /// </summary>
        public Decimal ConsumeTotal;
        /// <summary>
        /// 当前余额
        /// </summary>
        public Decimal NowLeftMoney;
        /// <summary>
        /// 可用金额
        /// </summary>
        public Decimal CanUseMoney;
        /// <summary>
        /// 密码模式
        /// </summary>
        public int PassMode;
        /// <summary>
        /// 档案编号
        /// </summary>
        public String FileNo;

        /// <summary>
        /// 源单内码
        /// </summary>
        public String SrcID;
        /// <summary>
        /// 源单类型
        /// </summary>
        public String SrcType;

        /// <summary>
        /// 付款方式
        /// </summary>
        public String PayWay;
        /// <summary>
        /// 结算方式
        /// </summary>
        public String BanlanceWay;
        /// <summary>
        /// 提货密码
        /// </summary>
        public String GetPassWord;
        /// <summary>
        /// 销售区域
        /// </summary>
        public String SalesArea;
        /// <summary>
        /// 制单人
        /// </summary>
        public String Biller;
        /// <summary>
        /// 审核人
        /// </summary>
        public String Checker;
        /// <summary>
        /// 调价单
        /// </summary>
        public String ListAdjustPrice;

        /// <summary>
        /// EAS财务组织
        /// </summary>
        public String FiUnitID;
    }
    /// <summary>
    /// 销售账户单据体
    /// </summary>
    public class E02A07E
    {
        public String BillId = String.Empty; //单据ID
        /// <summary>
        /// 产品
        /// </summary>
        public String ProInfo;
        /// <summary>
        /// 执行政策
        /// </summary>
        public String ExecPolicy;
        /// <summary>
        /// 含税单价
        /// </summary>
        public Decimal TaxPrice;
        
    }
}
